package com.tbsoft.ehr.login.service;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tbsoft.ehr.login.LoginInfo;
import com.tbsoft.ehr.login.UserInfo;
import com.tbsoft.ehr.login.dao.LoginDao;
import com.tbsoft.ehr.util.ConfigProps;

public class LoginServiceImpl implements LoginService {
	private Log log = LogFactory.getLog(getClass());

	@Override
	public boolean AutherUserInfo(HttpServletRequest httpRequest) {
		boolean auth = false;
		String j_username = httpRequest.getParameter("j_username");
		String j_password = httpRequest.getParameter("j_password");
		log.info("AutherUserInfo:" + j_username);
		UserInfo userInfo = loginDao.getUserInfoById(j_username, j_password);
		if (userInfo == null) {
			return false;
		}
		if ("S".equals(ConfigProps.getContextProperty("ehr.password.model"))) {
			auth = userInfo.getAuthPassword().equals(userInfo.getPassword());
		} else {
			auth = j_password.equals(userInfo.getPassword());
		}
		if (auth) {
			LoginInfo loginInfo = (LoginInfo) httpRequest.getSession().getAttribute("loginInfo");
			loginInfo.setUserId(userInfo.getUserId());
			loginInfo.setUserName(userInfo.getUserName());
			// httpRequest.setAttribute("loginInfo", loginInfo);
		}
		return auth;
	}

	@Override
	public UserInfo getUserInfoById(String userId) {
		UserInfo ui = loginDao.getUserInfoById(userId);
		return ui;

	}

	private LoginDao loginDao;

	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}

}
